package io.command;

import utils.Log;
import utils.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Created by shane on 2018/5/21.
 */
public class OsExcute {

    public static void command(String command) {
        if (StringUtils.isEmpty(command)) {
            return;
        }

        boolean err = false;
        try {
            Process process = new ProcessBuilder(command.split(" ")).start();
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String s;
            while ((s = in.readLine()) != null) {
                Log.vn(s);
            }

            BufferedReader errors = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((s = errors.readLine()) != null) {
                Log.vn(s);
                err = true;
            }

        } catch (Exception e) {
            e.printStackTrace();
            if (!command.startsWith("CMD /C")) {
                command("CMD /C " + command);
            } else {
                throw new RuntimeException(e);
            }
        }

        if (err) {
            throw new OsExecuteEcecption("Errors excuting " + command);
        }
    }
}
